﻿using Koala.Pro.Core.Models;

namespace Koala.Pro.Core.Dto
{
    [Serializable]
    public class DictTypeNodeDto : DictTypeInfo
    {
        public List<DictTypeNodeDto> Children { get; set; }

        public DictTypeNodeDto()
        {
            Children = new List<DictTypeNodeDto>();
        }

        public DictTypeNodeDto(DictTypeInfo typeInfo)
            : this()
        {
            base.Id = typeInfo.Id;
            base.Name = typeInfo.Name;
            base.Code = typeInfo.Code;
            base.Remark = typeInfo.Remark;
            base.Seq = typeInfo.Seq;
            base.PID = typeInfo.PID;
        }

        public DictTypeNodeDto Search(Func<DictTypeNodeDto, bool> predicate)
        {
            if (Children == null || Children.Count == 0)
            {
                if (predicate(this))
                {
                    return this;
                }
                return null;
            }
            List<DictTypeNodeDto> list = (from i in Children
                                          select i.Search(predicate) into i
                                          where i != null
                                          select i).ToList();
            if (list.Any())
            {
                DictTypeNodeDto obj = (DictTypeNodeDto)MemberwiseClone();
                obj.Children = list;
                return obj;
            }
            return null;
        }
    }
}
